package com.android.qjpmc.application_module.home.fragment;

import android.graphics.Color;
import android.os.Handler;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.Spinner;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.qjpmc.R;
import com.android.qjpmc.application_module.home.contract.HomeContract;
import com.android.qjpmc.application_module.home.viewModel.HomeViewModel;
import com.android.qjpmc.bean.AlarmRecordItemBean;
import com.android.qjpmc.bean.EventRecordItemBean;
import com.android.qjpmc.bean.ReportDataItemBean;
import com.android.qjpmc.constant.Constant;
import com.android.qjpmc.data_module.CSpinnerData;
import com.android.qjpmc.data_module.PwrDataModule;
import com.android.qjpmc.data_module.PwrDevice;
import com.android.qjpmc.data_module.PwrStation;
import com.android.qjpmc.databinding.FragementReportDataItemBinding;
import com.android.qjpmc.databinding.FragmentAlarmRecordItemBinding;
import com.android.qjpmc.databinding.FragmentAlarmRecordsBinding;
import com.android.qjpmc.databinding.FragmentStationEventDataBinding;
import com.android.qjpmc.manager.IntentManager;
import com.android.qjpmc.application_module.home.viewModel.AlarmRecordsViewModel;
import com.android.qjpmc.widget.databindingadapter.BaseBindingItemPresenter;
import com.android.qjpmc.widget.databindingadapter.BaseDataBindingDecorator;
import com.android.qjpmc.widget.databindingadapter.SingleTypeBindingAdapter;
import com.android.qjpmc.widget.mvvm.factory.CreateViewModel;
import com.android.qjpmc.widget.mvvm.view.BaseMVVMFragment;
import com.android.qjpmc.widget.utils.TimeUtils;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.util.ArrayList;
import java.util.Date;

@CreateViewModel(HomeViewModel.class)
public class ViewEventFragment extends BaseMVVMFragment<HomeViewModel, FragmentStationEventDataBinding> implements HomeContract.View, BaseBindingItemPresenter {

    PwrStation m_the_power_station = null;
    PwrStation GetThePwrStation()
    {
        return m_the_power_station;
    }
    void SetPwrStation(PwrStation station)
    {
        m_the_power_station = station;
    }
    int m_current_station_id = 0;
    int m_current_device_id = 0;
    int m_all_time = 1;
    String m_start_time;
    String m_end_time;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_station_event_data;
    }

    @Override
    public void initView() {
        mBinding.setView(this);
       // InitializeStationSpinner();
        InitializeCheckBox();

    }

    @Override
    public void initData()
    {
        PwrDataModule.getInstance().SetEventFragment(this);
    }

    @Override
    public void initEvent() {

    }






    public void openData() {


    }

    public void onSelectQueryCondition()
    {
        IntentManager.toSelectAlarmDataActivity(mActivity);

    }

    public void onQueryRecordData()
    {
        UpdataValuesText();
    }
    public void onWaringDetails() {
        IntentManager.toAlarmRecordDetailActivity(getContext());
    }

    void UpdataValuesText()
    {
        //if ( (m_thePwrDevice != null) && (m_CurrentPwrStation != null) )
           /* ArrayList<EventRecordItemBean> AnalogList = PwrDataModule.getInstance().GetEventRecordDataArray();//PwrDataModule.getInstance().GetAnalogDataList(m_CurrentPwrStation.getM_ID(), m_thePwrDevice.GetID() );
            SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(getContext(), AnalogList, R.layout.fragment_alarm_record_item);
            mBinding.rtDataList.setAdapter(adapter);

            adapter.setItemPresenter(this);

            mBinding.rtDataList.setLayoutManager(new LinearLayoutManager(getContext()));


            adapter.setItemDecorator(new BaseDataBindingDecorator<EventRecordItemBean, FragmentAlarmRecordItemBinding>() {

                @Override
                public void decorator(FragmentAlarmRecordItemBinding binding, int position, int viewType, EventRecordItemBean data) {
                    binding.time.setText(data.m_time);
                    binding.position.setText(data.m_name);
                    binding.value.setText(data.m_value_text);
                    binding.faultValue.setText(data.m_fault_value);
                }
*//*
                @Override
                public void decorator( FragmentAlarmRecordItemBinding, int position, int viewType, ReportDataItemBean data) {
                    binding.tvName.setText(data.name);
                    binding.tvUnit.setText(data.unit);
                    binding.tvValue.setText(data.value );
                    binding.tvTime.setText(data.time );

                    binding.tvIndex.setText(Integer.toString(position + 1));
                    binding.tvName.setGravity(Gravity.CENTER);
                    binding.tvUnit.setGravity(Gravity.CENTER);
                    binding.tvValue.setGravity(Gravity.CENTER);
                    binding.tvIndex.setGravity(Gravity.CENTER);
                    binding.tvTime.setGravity(Gravity.CENTER);
                    if (position % 2 == 0)
                    {
                        binding.tvTime.setBackgroundColor(Color.rgb(219, 238, 244));
                        binding.tvName.setBackgroundColor(Color.rgb(219, 238, 244));
                        binding.tvUnit.setBackgroundColor(Color.rgb(219, 238, 244));
                        binding.tvValue.setBackgroundColor(Color.rgb(219, 238, 244));
                        binding.tvIndex.setBackgroundColor(Color.rgb(219, 238, 244));
                    }

                }*//*
            });*/
    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }

    int InitializeStationSpinner()
    {
        CSpinnerData data = null;
        data = new CSpinnerData(-1, "所有站");
        ArrayList<CSpinnerData> data_list = new ArrayList<CSpinnerData>();

        data_list.add(data);
        //data_list.add("月报表");
        //data_list.add("年报表");

        PwrStation station = PwrDataModule.getInstance().GetFirstStation();
        while (station != null)
        {
            data = new CSpinnerData(station.getM_ID(), station.getM_Name());
            data_list.add(data);
            station = station.GetNext();
        }
        //适配器
        ArrayAdapter<CSpinnerData> arr_adapter= new ArrayAdapter<CSpinnerData>(mActivity,R.layout.spinner_layout, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        //加载适配器
        mBinding.stationSpinner.setAdapter(arr_adapter);

        mBinding.stationSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

                                                              @Override
                                                              public void onItemSelected(AdapterView<?> arg0, View arg1,
                                                                                         int arg2, long arg3) {
                                                                  Log.d("mark", "onItemSelected() is invoked!");
                                                                  arg0.setVisibility(View.VISIBLE);
                                                                  m_current_station_id = ((CSpinnerData)((Spinner)arg0).getSelectedItem()).GetID();
                                                                  if (m_current_station_id != -1)
                                                                  {
                                                                      PwrStation station = PwrDataModule.getInstance().GetPwrStationByID(m_current_station_id);
                                                                      SetPwrStation(station);
                                                                  }
                                                                  InitializeDevicesSpinner();
                                                              }

                                                              @Override
                                                              public void onNothingSelected(AdapterView<?> arg0)
                                                              {
                                                              }
                                                          }
        );
        return 0;
    }
    int InitializeDevicesSpinner()
    {
        CSpinnerData data = null;
        PwrStation the_powr_station = GetThePwrStation();
        ArrayList<CSpinnerData> data_list = new ArrayList<CSpinnerData>();

        if (the_powr_station == null)
        {
            if (mBinding.deviceSpinner.getChildCount() > 0)
            {
                mBinding.deviceSpinner.removeAllViews();
            }
            return 1;
        }
        if (m_current_station_id == -1)
        {
            if (mBinding.deviceSpinner.getChildCount() > 0)
            {
                mBinding.deviceSpinner.removeAllViews();
            }
            return 1;
        }
        PwrDevice the_device = the_powr_station.GetFirstDevice();
        data = new CSpinnerData(-1, "所有设备");
        data_list.add(data);
        while (the_device != null)
        {
            data = new CSpinnerData(the_device.GetID(), the_device.getM_Name());
            data_list.add(data);
            the_device = the_device.GetNext();
        }
        ArrayAdapter<CSpinnerData> arr_adapter= new ArrayAdapter<CSpinnerData>(this.getContext(), R.layout.spinner_layout, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(android.R.layout.simple_list_item_checked);
        //加载适配器
        mBinding.deviceSpinner.setAdapter(arr_adapter);

        mBinding.deviceSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                Log.d("mark", "onItemSelected() is invoked!");
                arg0.setVisibility(View.VISIBLE);
                m_current_device_id = ((CSpinnerData)((Spinner)arg0).getSelectedItem()).GetID();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {

            }
        });

        return 0;
    }

    TimePickerView pvStartTime;
    TimePickerView pvEndTime;

    public void onSelectStartTime() {
        if (pvStartTime == null) {
            pvStartTime = new TimePickerBuilder(this.getContext(), new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    m_start_time = TimeUtils.getParseDayTextTime(date);
                    mBinding.beginTime.setText(m_start_time);
                }
            }).setType(new boolean[]{true, true, true, true, true, false})
                    .build();
        }
        pvStartTime.show();
    }

    public void onSelectEndTime() {
        if (pvEndTime == null) {
            pvEndTime = new TimePickerBuilder(this.getContext(), new OnTimeSelectListener() {
                @Override
                public void onTimeSelect(Date date, View v) {
                    m_end_time = TimeUtils.getParseDayTextTime(date);
                    mBinding.endTime.setText(m_end_time);
                }
            }).setType(new boolean[]{true, true, true, true, true, false})
                    .build();
        }
        pvEndTime.show();
    }

    void InitializeCheckBox()
    {
        mBinding.ckSelectAllTime.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)
                {
                    m_all_time = 1;
                    mBinding.beginTime.setEnabled(false);
                    mBinding.endTime.setEnabled(false);
                    mBinding.beginTime.setClickable(false);
                    mBinding.endTime.setClickable(false);
                }
                else
                {
                    mBinding.beginTime.setEnabled(true);
                    mBinding.endTime.setEnabled(true);

                    mBinding.beginTime.setClickable(true);
                    mBinding.endTime.setClickable(true);
                    m_all_time = 0;

                }

            }
        });
        m_all_time = 1;
        mBinding.beginTime.setEnabled(false);
        mBinding.endTime.setEnabled(false);

        mBinding.beginTime.setClickable(false);
        mBinding.endTime.setClickable(false);
        mBinding.ckSelectAllTime.setChecked(true);
    }

    private Handler m_handler = new Handler();

    Runnable m_UpdateUIRunnable = new Runnable(){
        @Override
        public void run()
        {
            InitializeStationSpinner();
            //returnSiteStationData(PwrDataModule.getInstance().GetStaionBeanList() );
        }
    };

    public int UpdateStationListUI()
    {
        if (m_handler == null)
        {
            return Constant.GEN_ERROR;
        }
        m_handler.post(m_UpdateUIRunnable);
        return Constant.NO_ERROR;
    }


}
